package com.xinfu.newproject.widget.picture;

import android.annotation.SuppressLint;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import androidx.annotation.Nullable;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SimpleTarget;
import com.xinfu.newproject.R;
import com.xinfu.newproject.base.BaseActivity;
import com.xinfu.newproject.widget.imagepicker.util.SystemUtil;
import com.xinfu.newproject.widget.imagepicker.widget.ClipImageLayout;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class PictureViewActivity extends BaseActivity {

    @BindView(R.id.clip_layout)
    ClipImageLayout imageView;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_photo);
        ButterKnife.bind(this);
        initViewBind();
        initLisener();
    }

    private void initLisener() {

    }

    private void initViewBind() {
        if (getIntent().getExtras().getString("picture") != null) {
//            Glide.with(this).load(getIntent().getExtras().getString("picture")).asBitmap().into(imageView);
            startCrop();
        }
    }

    @OnClick({R.id.activity_picture_cancel})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.activity_picture_cancel:
                finish();
                break;
        }
    }


    Bitmap myBitmap = null;

    @SuppressLint("StaticFieldLeak")
    private void startCrop() {

        if (!TextUtils.isEmpty(getIntent().getExtras().getString("picture"))) {

            Glide.with(this).load(getIntent().getExtras().getString("picture")).asBitmap().into(new SimpleTarget<Bitmap>() {
                @Override
                public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
                    if (resource != null) {
                        SystemUtil.runOnUIThread(new Runnable() {
                            @Override
                            public void run() {
                                startCrop(resource);
                            }
                        });
                    }
                }
            });

//            new AsyncTask<Void, Void, Void>() {
//                @Override
//                protected Void doInBackground(Void... params) {
//                    try {
//                        myBitmap = Glide.with(PictureViewActivity.this)
//                                .load(getIntent().getExtras().getString("picture"))
//                                .asBitmap() //必须
//                                .centerCrop()
//                                .into(500, 800)
//                                .get();
//                    } catch (ExecutionException e) {
//                        e.printStackTrace();
//                    } catch (InterruptedException e) {
//                        e.printStackTrace();
//                    }
//
//                    if (myBitmap != null) {
//                        SystemUtil.runOnUIThread(new Runnable() {
//                            @Override
//                            public void run() {
//                                startCrop(myBitmap);
//                            }
//                        });
//                    }
//
//                    return null;
//                }
//            }.execute();
        }

    }

    private void startCrop(Bitmap bitmap) {
        imageView.setImageBitmap(bitmap);
    }

}